package com.xxl.rpc.sample.client.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Service
public class KafkaService {

    @Autowired
    private KafkaTemplate kafkaTemplate;
    @RequestMapping("kafka")
    @ResponseBody
    public void kafka(String topic,String value) {
        ListenableFuture<SendResult<String, Object>> listenableFuture= kafkaTemplate.send(topic,value);
        listenableFuture.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("消息发送失败");
            }

            @Override
            public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
                System.out.println("消息发送成功");
            }
        });

    }
}
